//
//  AboutUsViewController.m
//  PalmChangtu
//
//  Created by shaorui on 15/6/18.
//  Copyright (c) 2015年 PalmChangtu. All rights reserved.
//

#import "AugreementWebViewController.h"
#import "MainTabbarController.h"
#import "BaseUrlConfig.h"
#import "JSONKit.h"
#import "Common.h"
#import "LoadingView.h"
#import "NetWarningView.h"
#import "AFNetworking.h"
#import "BaseUrlConfig.h"
#import "ShareUserModel.h"
@interface AugreementWebViewController ()
{
    BOOL RequestBool;

    
}
@end

@implementation AugreementWebViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
    }
    return self;
}


-(void)viewDidAppear:(BOOL)animated
{
    
    if (RequestBool) {
        RequestBool = NO;
        [self RequestRec];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    RequestBool = YES;
    
    [_v_Top.lalTitel setText:_strTypeName];
    
    [_v_Top.btnGoback setHidden:NO];
    _v_Top.parentController = self.navigationController;
//    [_v_Top.btnGoback removeTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside];
//    
//    [_v_Top.btnGoback addTarget:self action:@selector(btnBack:) forControlEvents:UIControlEventTouchUpInside];
//
//    NSURL *  url =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HostServer,statementAaction]];
//    
//    
    [_webView setDelegate:self];
//
//    NSURLRequest *request=[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.baidu.com"]]];
//    
//    
//    [self.webView loadRequest:request];

    
    

    [self.webView setScalesPageToFit:NO];


    
    
    
    
    // Do any additional setup after loading the view from its nib.
}
-(void)btnBack:(UIButton *)back
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    //    添加指示器
//    UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    aiv.tag = 1000;
//    aiv.frame = CGRectMake((_webView.frame.size.width-20)/2, (_webView.frame.size.height-20)/2, 20, 20);
//    [aiv startAnimating];
//    [_webView addSubview:aiv];


}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    // finished loading, hide the activity indicator in the status bar
//    UIActivityIndicatorView *aiv = (UIActivityIndicatorView*)[_webView viewWithTag:1000];
//    [aiv stopAnimating];
//    aiv = nil;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

//    UIActivityIndicatorView *aiv = (UIActivityIndicatorView*)[_webView viewWithTag:1000];
//    [aiv stopAnimating];
//    aiv = nil;
}

#pragma mark - network request

-(void)RequestRec
{

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setTimeoutInterval:10];
//
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//
    manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//响应
    NSString *strUrl = [NSString stringWithFormat:@"%@%@",HostServer,statementAaction];
    
    
    ShareUserModel * shareUser  = [ShareUserModel shareUserModel];
    
    
    
    NSDictionary *params = @{
                             
                             @"type":[NSString stringWithFormat:@"%@",self.strTypeID],
                             @"loginName":[NSString stringWithFormat:@"%@",shareUser.user.phone],
                             @"sessionId":[NSString stringWithFormat:@"%@",shareUser.user.sessionId]
                             
                             };
    
    
    
    [manager POST:strUrl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         
         
         //               NSData *doubi = responseObject   ;
         //                   //     JSON格式的NSString转换成NSMutableDictionary
         //               NSString *strImge =  [[NSString alloc]initWithData:doubi encoding:NSUTF8StringEncoding];
         //
         //               NSError *err = nil;
         //               NSDictionary *dicRespon  = [NSJSONSerialization JSONObjectWithData:[strImge dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
         
         NSDictionary * dicRespon = responseObject;
         
         [self RequestCommplte:dicRespon];
         
         
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         [[LoadingView shareLoadingView] dismissAnimated:YES];
         [[NetWarningView sharedNetWarningView] show];  //警告网络异常
         //              NSData *doubi = operation.error    ;
         //                  //     JSON格式的NSString转换成NSMutableDictionary
         //              NSString *strImge =  [[NSString alloc]initWithData:doubi encoding:NSUTF8StringEncoding];
         //
         //              NSError *err = nil;
         //              NSDictionary *dicRespon  = [NSJSONSerialization JSONObjectWithData:[strImge dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
         NSLog(@"Error: %@", error);
         NSLog(@"请求出错了: - %@ \n%@",
               [error localizedDescription],
               [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
     }];
    
}

-(void)RequestCommplte:(NSDictionary *)dicRespon
{

    NSString * strJson = [dicRespon JSONString];
    [self.webView loadHTMLString:strJson baseURL:nil];


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
